import screepsapi
import time
from PIL import Image, ImageDraw, ImageFont
import datetime

TOKEN = "6a49ab20-1cf4-4b11-afe4-e7f8541529f6"
api = screepsapi.API(token=TOKEN)
temp = ''


# def nukers():
#     print("获取核弹数据")
#     nuker2 = api.nukes()
#     print(nuker2)
#     print(nuker2['nukes'])
#     nukers = nuker2['nukes']
#     print(nukers['shard0'])
#     s0nukers = nukers['shard0']
#     print(s0nukers[1]['room'])
#     print('Shard0')
#     for mix in s0nukers:
#         # print(mix['launchRoomName'],
#         #       "[", api.room_overview(mix['launchRoomName'], shard=shard_name)['owner']['username'], "]",
#         #       "=>",
#         #       mix['room'],
#         #       "[", api.room_overview(mix['room'], shard=shard_name)['owner']['username'], "]",
#         #       ":", mix['landTime'] - game_time)
#         pass


def print_nuke(shard_name):
    print("获取时间")
    game_time = api.time(shard_name)
    time.sleep(5)
    print(game_time)
    print("获取核弹数据")
    nuker = api.nukes()
    time.sleep(5)
    # print(nuker2)
    # print(nuker2['nukes'])
    nukers = nuker['nukes']
    # print(nukers[shard_name])
    s0nukers = nukers[shard_name]
    # print(s0nukers[1]['room'])
    global temp
    temp += "======================" + shard_name + "======================" + "\n"
    print(shard_name)
    for mix in s0nukers:
        time.sleep(5)
        try:
            result = mix['launchRoomName'] + " [ " + \
                     api.room_overview(mix['launchRoomName'], shard=shard_name)['owner'][
                         'username'] + " ] " + "=> " + mix['room'] + " [ " + \
                     api.room_overview(mix['room'], shard=shard_name)['owner']['username'] + " ] " + ": " + str(
                mix['landTime'] - game_time)
            # print(result)
            temp += result + '\n'
        except:
            result = mix['launchRoomName'] + " [ " + \
                     api.room_overview(mix['launchRoomName'], shard=shard_name)['owner'][
                         'username'] + " ] " + "=> " + mix['room'] + " [ ? ] " + ": " + str(
                mix['landTime'] - game_time)
            # print(result)
            temp += result + '\n'
    print(temp)


def print_nuke_new(shard_name):
    print("获取{}时间".format(shard_name))
    game_time = api.time(shard_name)
    time.sleep(5)
    print(game_time)
    print("获取核弹数据")
    nuker = api.nukes()
    time.sleep(5)
    # print(nuker2)
    # print(nuker2['nukes'])
    nukers = nuker['nukes']
    # print(nukers[shard_name])
    s0nukers = nukers[shard_name]
    # print(s0nukers[1]['room'])
    global temp
    temp += "======================" + shard_name + "======================" + "\n"
    print(shard_name)
    for mix in s0nukers:
        time.sleep(5)
        try:
            launchRoomName = api.room_overview(mix['launchRoomName'], shard=shard_name)['owner']['username']
        except:
            launchRoomName = "?"
        time.sleep(5)
        try:
            room = api.room_overview(mix['room'], shard=shard_name)['owner']['username']
        except:
            room = "?"
        time.sleep(5)
        result = (mix['launchRoomName'] + " [ " + launchRoomName + " ] " + "=> " +
                  mix['room'] + " [ " + room + " ] " + ": " +
                  str(mix['landTime'] - game_time))
        print(result)
        temp += result + '\n'
    print(temp)


def runapp():
    print("开始获取数据")
    print_nuke_new('shard0')
    time.sleep(5)
    print_nuke_new('shard1')
    time.sleep(5)
    print_nuke_new('shard2')
    time.sleep(5)
    print_nuke_new('shard3')
    time.sleep(5)


def save2(text_input):
    newline_count = text_input.count("\n")
    print("字符串中换行符的次数：", newline_count)
    linepix = int(1024 * (newline_count + 4) / 32)
    # 创建一个空白图像
    width, height = 1080, linepix
    background_color = (47, 79, 79)
    image = Image.new("RGB", (width, height), background_color)
    draw = ImageDraw.Draw(image)

    # 在图像上绘制文本
    text = text_input
    font_size = 30
    font = ImageFont.truetype("Arial.ttf", font_size)  # 使用合适的字体文件路径
    text_color = (192, 192, 192)
    text_position = (50, 50)
    draw.text(text_position, text, fill=text_color, font=font)

    # 保存图像
    image.save("/www/wwwroot/pythonproject/nuker2/nukers2.png")
    image.save("/www/wwwroot/nuker2.com/nukers2.png")
    print("save png")


def save(text_input):
    newline_count = text_input.count("\n")
    print("字符串中换行符的次数：", newline_count)
    linepix = int(1024 * (newline_count + 4) / 32)
    # 创建一个空白图像
    width, height = 1080, linepix
    background_color = (47, 79, 79)
    image = Image.new("RGB", (width, height), background_color)
    draw = ImageDraw.Draw(image)

    # 在图像上绘制文本
    text = text_input
    font_size = 30
    font = ImageFont.truetype("Arial.ttf", font_size)  # 使用合适的字体文件路径
    text_color = (192, 192, 192)
    text_position = (50, 50)
    draw.text(text_position, text, fill=text_color, font=font)

    # 保存图像
    image.save("/www/wwwroot/pythonproject/nuker2/nukers.png")
    image.save("/www/wwwroot/nuker2.com/nukers.png")
    print("save png")


def runmain():
    print('爬爬脚本启动')
    save2("loding...\nOK")
    start_time = time.time()
    user = api.me()
    time.sleep(5)
    print("使用", user['username'], "的Key进行爬取")
    game_time_now = api.time('shard3')
    time.sleep(5)
    print("当前S3游戏时间为", game_time_now)
    runapp()
    end_time = time.time()
    print("本轮此请求请求耗时: {:.2f}秒".format(end_time - start_time))
    global temp
    temp += "$$$takes {:.2f} seconds$$$\n".format(end_time - start_time)
    current_time = datetime.datetime.now()
    print(current_time)
    temp += "$$$" + str(current_time) + "$$$"
    save(temp)


if __name__ == '__main__':
    try:
        runmain()
        print(temp)
    except:
        print("crush")
